
export default function setTheme() {

  // const colorSetting = {
  //   primary: "#409eff",
  //   success: "#00C759",
  //   warning: "#ffab00",
  //   danger: "#FF0033",
  //   error: "#FF0033",
  //   info: "#73E9EF",
  // }
  const colorSetting = {
    primary: "#673ab7",
    success: "#009688",
    warning: "#ff9800",
    danger: "#f44336",
    error: "#f44336",
    info: "#607d8b",
  }


  setColorVarList(colorSetting)
}





function setColorVarList(colorSetting: { [key: string]: string }, isDark: boolean = false) {
  let colorVarList = {} as any
  Object.keys(colorSetting).map(name => {
    const c = colorSetting[name]
    colorVarList = {
      ...colorVarList,
      ...generateThemeColorOne(name, c, isDark)
    }
  })

  Object.keys(colorVarList).map(name => {
    const color = colorVarList[name]
    document.documentElement.style.setProperty(name, color)
  })
}




function hexToRgb(hex: string) {
  // 将 # 号去掉
  hex = hex.replace("#", "");
  // 如果 hex 长度不为 6，则返回 null
  if (hex.length !== 6) {
    return {
      r: 255,
      g: 255,
      b: 255,
    };
  }
  // 将 hex 分成 r、g、b 三个部分
  let r = parseInt(hex.substring(0, 2), 16);
  let g = parseInt(hex.substring(2, 4), 16);
  let b = parseInt(hex.substring(4), 16);
  // 返回 R、G、B 组成的字符串
  return { r, g, b }
}



/**
 * 生成单个样式的主题色
 */
function generateThemeColorOne(
  name = "primary",
  color = "#6E00FF",
  isDark: boolean,
) {

  const { r, g, b } = hexToRgb(color)

  const getPerColor = (r: number, g: number, b: number, a: number, dark: boolean) => {
    if (dark) {
      r = Math.round(r * (1 - a))
      g = Math.round(g * (1 - a))
      b = Math.round(b * (1 - a))
    } else {
      r = r + Math.round((255 - r) * a)
      g = g + Math.round((255 - g) * a)
      b = b + Math.round((255 - b) * a)
    }
    return `rgb(${r},${g},${b})`
  }

  return {
    [`--el-color-${name}`]: color,
    [`--el-color-${name}-light-3`]: getPerColor(r, g, b, 0.3, isDark),
    [`--el-color-${name}-light-5`]: getPerColor(r, g, b, 0.5, isDark),
    [`--el-color-${name}-light-7`]: getPerColor(r, g, b, 0.7, isDark),
    [`--el-color-${name}-light-8`]: getPerColor(r, g, b, 0.8, isDark),
    [`--el-color-${name}-light-9`]: getPerColor(r, g, b, 0.9, isDark),
    [`--el-color-${name}-dark-2`]: getPerColor(r, g, b, 0.2, !isDark),
  }
}